#include <iostream>

#include "../include/mempool.h"

using namespace std;

/// @brief 测试类
class Actual
{
private:
    static int count;
    int num;

public:
    Actual()
    {
        num = count;
        count++;
    }

    void print()
    {
        cout << this << ": the " << num << " object" << endl;
    }

    void *operator new(size_t size);
    void operator delete(void *p);
};

int Actual::count = 0;

/// @brief 内存池对象
mempool<sizeof(Actual), 2> mp;

void *Actual::operator new(size_t size)
{
    return mp.malloc();
}

void Actual::operator delete(void *p)
{
    mp.free(p);
}

int main()
{
    Actual *p1 = new Actual;
    p1->print();

    Actual *p2 = new Actual;
    p2->print();

    delete p1;
    p1 = new Actual;
    p1->print();

    Actual *p3 = new Actual;
    p3->print();

    delete p1;
    delete p2;
    delete p3;
    return 0;
}
